from django import template
 
register = template.Library()

class SetVarNode(template.Node):
	def __init__(self, var_name, var_value, var_value2):
		self.var_name = var_name
		self.var_value = var_value
		self.var_value2 = var_value2

	def render(self, context):
		try:
			value = template.Variable(self.var_value).resolve(context) - int(self.var_value2)
		except template.VariableDoesNotExist:
			value = ""
		context[self.var_name] = value
		return u""

def set_var(parser, token):
	"""
		{% set <var_name>  = <var_value> - <var_value2> %}
	"""
	parts = token.split_contents()
	if len(parts) < 6:
		raise template.TemplateSyntaxError("'set' tag must be of the form:  {% set <var_name>  = <var_value> - <var_value2> %}")
	return SetVarNode(parts[1], parts[3], parts[5])

register.tag('set', set_var)